package com.yuexun.beilunpatient.ui.login.ui;

import android.content.Intent;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.bean.BaseListEntity;
import com.yuexun.beilunpatient.ui.login.bean.LoginBean;
import com.yuexun.beilunpatient.ui.login.bean.UserDtlBean;
import com.yuexun.beilunpatient.ui.login.model.impl.LoginModel;
import com.yuexun.beilunpatient.ui.login.presenter.impl.LoginPresenter;
import com.yuexun.beilunpatient.ui.login.presenter.impl.UserDtlPresenter;
import com.yuexun.beilunpatient.ui.login.ui.view.ILoginView;
import com.yuexun.beilunpatient.ui.login.ui.view.IUserDtlView;
import com.yuexun.beilunpatient.ui.main.ui.MainActivity;
import com.yuexun.beilunpatient.widget.LoadingDialog;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.ButterKnife;

/**
 * Created by work-jx on 2016/6/30.
 */
public class Act_Start extends KJActivity implements ILoginView,IUserDtlView {
    private LoadingDialog dialog;
    private LoginPresenter presenter;
    UserDtlPresenter userDtlPresenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_start);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();

        login();
    }

    private void login() {
        Boolean islogin = PreferenceHelper.readBoolean(this, AppConfig.USER_INFO, AppConfig.ISLOGIN);
        if (!islogin) {              //重新登陆
            Timer timer = new Timer();
            TimerTask tast = new TimerTask() {
                @Override
                public void run() {
                    showActivity(aty, LoginActivity.class);
                    finish();
                }
            };
            timer.schedule(tast, 2000);
        } else {                    //已登陆
            presenter = new LoginPresenter(new LoginModel(), this);
            userDtlPresenter = new UserDtlPresenter(new LoginModel(),this);
            Timer timer = new Timer();
            TimerTask tast = new TimerTask() {
                @Override
                public void run() {
                    Map<String, String> params = new HashMap<>();
                    params.put("idCard", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USERNAME_LOGIN));
                    params.put("password", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USERPWD));
                    params.put("deviceToken", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USERDEVICE));
                    params.put("appKey", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USERAPPKEY));
                    presenter.dologin(params);
                }
            };
            timer.schedule(tast, 1000);
        }
    }

    @Override
    public void doInLogin(BaseListEntity<LoginBean> loginBean) {
        if (loginBean.getStatus().equals("200")) {
            if(loginBean.getDatas().get(0).getCode().equals("1")){
                String filename = AppConfig.USER_INFO;
                PreferenceHelper.write(aty, filename, AppConfig.PICTURE_UUID, loginBean.getDatas().get(0).getPictureUuid());
                PreferenceHelper.write(aty, filename, AppConfig.SESSION_ID, loginBean.getDatas().get(0).getSessionUuid());
                PreferenceHelper.write(aty, filename, AppConfig.USER_NAME, loginBean.getDatas().get(0).getUserName());
                PreferenceHelper.write(aty, filename, AppConfig.PATIENT_ID, loginBean.getDatas().get(0).getPatientId());

                loadUsrInfo();
            }else{
                ViewInject.toast(loginBean.getDatas().get(0).getDescription());
                showActivity(aty, LoginActivity.class);
                finish();
            }

        }else{
            ViewInject.toast(loginBean.getMessage());
            showActivity(aty, LoginActivity.class);
            finish();
        }
    }

    /**
     * 获取用户详情
     */
    private void loadUsrInfo(){
        Map<String, String> params = new HashMap<>();
        params.put("patientId", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.PATIENT_ID));
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        userDtlPresenter.loadPatientInfoDetail(params);
    }

    @Override
    public void fail() {
        showActivity(aty, LoginActivity.class);
        finish();
    }

    @Override
    public void loadPatientInfoDetail(BaseListEntity<UserDtlBean> entity) {
        if(entity.getStatus().equals("200")){
            String filename = AppConfig.USER_INFO;
            PreferenceHelper.write(aty, filename, AppConfig.TEL, entity.getDatas().get(0).getMobile());
            PreferenceHelper.write(aty, filename, AppConfig.HEALTHID, entity.getDatas().get(0).getHealthNo());
            if(entity.getDatas().get(0).getSex() == 1){
                PreferenceHelper.write(aty, filename, AppConfig.SEX, "男");
            }else if(entity.getDatas().get(0).getSex() == 2){
                PreferenceHelper.write(aty, filename, AppConfig.SEX, "女");
            }else {
                PreferenceHelper.write(aty, filename, AppConfig.SEX, "不详");
            }

            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
        else {
            ViewInject.toast("个人详情获取失败:"+entity.getMessage());
            showActivity(aty, LoginActivity.class);
            finish();
        }
    }

}
